//
//  YXImageVideoViewController.m
//  YXImageBrowserDemo
//
//  Created by guo peng huang on 2021/5/17.
//

#import "YXImageVideoViewController.h"
#import "YXImageBrowser.h"
#import "YXVideoData.h"

@interface YXImageVideoViewController ()

@end

@implementation YXImageVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

+ (NSString *)yx_title {
    return @"图片+视频";
}

- (void)initData {
    NSMutableArray *array = [NSMutableArray array];
    [array addObjectsFromArray:[YXBaseFileManager yx_imageURLs]];
    [array addObjectsFromArray:[YXBaseFileManager yx_imageNames]];
    [array addObjectsFromArray:[YXBaseFileManager yx_videos]];
    self.dataArray = array;
}

- (void)yx_selectedIndex:(NSInteger)index {
    NSLog(@"---%ld",index);
    NSMutableArray *datas = [NSMutableArray array];
    [self.dataArray enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj hasSuffix:@".mp4"] && [obj hasPrefix:@"http"]) {
            
            // 网络视频
            YXVideoData *data = [YXVideoData new];
            data.videoURL = [NSURL URLWithString:obj];
            data.projectiveView = [self viewAtIndex:idx];
            [datas addObject:data];
         
        } else if ([obj hasSuffix:@".mp4"]) {
            
            // 本地视频
            NSString *path = [[NSBundle mainBundle] pathForResource:obj.stringByDeletingPathExtension ofType:obj.pathExtension];
            YXVideoData *data = [YXVideoData new];
            data.videoURL = [NSURL fileURLWithPath:path];
            data.projectiveView = [self viewAtIndex:idx];
            [datas addObject:data];
            
        } else if ([obj hasPrefix:@"http"]) {
            
            // 网络图片
            YXImageData *data = [YXImageData new];
            data.imageURL = [NSURL URLWithString:obj];
            data.projectiveView = [self viewAtIndex:idx];
            [datas addObject:data];
            
        } else {
            
            // 本地图片
            YXImageData *data = [YXImageData new];
            data.imageName = obj;
            data.projectiveView = [self viewAtIndex:idx];
            [datas addObject:data];
            
        }
    }];
    
    YXImageBrowser *browser = [YXImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = index;
//    browser.autoHideProjectiveView = NO;
    // 只有一个保存操作的时候，可以直接右上角显示保存按钮
    browser.defaultToolViewHandler.topView.operationType = YXTopViewOperationTypeSave;
    [browser show];
}

@end
